<?php

namespace Admin\Controller;

use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;

class GoldController extends Controller {

    //增加减少金币
    public function gold_add_subtract(){

        if(IS_AJAX){

            $member = M('Member');
            $check_user = $member->where(array('member_tel' => I('member_tel')))->find();

            if (!$check_user) {
                $this->error('没找到该会员，请您查验后重新提交', 0, 0);
            }

            $data['member_id'] = $check_user['member_id'];

            $goldType = I('gold_type');

            if($goldType == 'add'){

                $logData['gold_type'] = '增加金币';

                $data['member_gold'] = $check_user['member_gold'] + I('gold_coins');

            }else if($goldType == 'subtract'){

                $logData['gold_type'] = '减少金币';

                $data['member_gold'] = $check_user['member_gold'] - I('gold_coins');

                if($data['member_gold'] < 0){
                    $this->error('该用户减少指定金币后，金币数量会成负值，请您重新填写数量', 0, 0);
                }

            }

            $member->save($data);

            $goldLog = M('GoldLog');

            $logData['gold_uid'] = $check_user['member_id'];
            $logData['gold_utel'] = $check_user['member_tel'];
            $logData['gold_unickname'] = $check_user['member_nickname'];
            $logData['gold_ugoldbe'] = $check_user['member_gold'];
            $logData['gold_ugoldaf'] = $data['member_gold'];
            $logData['gold_coins'] = I('gold_coins');
            $logData['add_time'] = time();
            $logData['display'] = 0;
            $logData['sort'] = 0;

            $goldLog->add($logData);

            $this->success('操作成功', U('gold_add_subtract_log_list'), 1);


        }else{
            $this->display();
        }

    }

    //增加减少金币记录
    public function gold_add_subtract_log_list(){

        $goldLog = M('GoldLog');
        $val = I('val');

        $this->assign('testval', $val);
        if ($val) {
            $map['gold_utel'] = array('like', "%" . $val . "%");
        }

        $count = $goldLog->where($map)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数

        foreach ($map as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show(); // 分页显示输出

        $gold_log_list = $goldLog->where($map)->order('gold_id')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('gold_log_list', $gold_log_list);
        $this->assign('page', $show);

        $this->display();

    }

}
